#include "Collecting.h"

#include "../CommandManager.h"
#include "../GameObjects/Room.h"
#include "../GameObjects/Character.h"
#include "../GameObjects/Item.h"
#include "../Player.h"

#include <iostream>

void Collecting::Init()
{
	CommandManager::getSingleton()->add(std::make_shared<Get>());
	CommandManager::getSingleton()->add(std::make_shared<Drop>());
}

///////////////////////////////////////
// LOOK
///////////////////////////////////////
Get::Get() : Command("get")
{
}
void Get::execute(const CharacterPtr &character, const RoomPtr &room, const std::vector<std::string> &args)
{
	if(args.empty())
	{
		if(character->hasPlayer())
			std::cout << "What do you want to pick up?" << std::endl;
		return;
	}

	if(args.size() == 1)
	{
		for(unsigned int i = 0; i < room->getObjects().size(); i++)
		{
			//If argument is a character name, then look at the character.
			if(room->getObjects()[i]->is(args[0]))
			{
				ItemPtr item = std::dynamic_pointer_cast<Item>(room->getObjects()[i]);
				if(item)
				{
					room->remove(item);
					character->add(item);
					item->setStance(Item::IN_POCKET);

					if(character->hasPlayer())
						std::cout << "You put " << item->getIndefiniteArticle() << " " << item->getName() << " in your pocket." << std::endl;
					else
						std::cout << character->getName() << " puts " << item->getIndefiniteArticle() << " " << item->getName() << " in his pocket." << std::endl;
				}
				else if(character->hasPlayer())
				{
					std::cout << "Can't find that here..." << std::endl;
				}
				return;
			}
		}

		if(character->hasPlayer())
		{
			std::cout << "You can't pick up that!" << std::endl;
			return;
		}
	}

	if(character->hasPlayer())
		std::cout << "You don't know how to do that!" << std::endl;
}

///////////////////////////////////////
// EXAMINE
///////////////////////////////////////
Drop::Drop() : Command("drop")
{
}
void Drop::execute(const CharacterPtr &character, const RoomPtr &room, const std::vector<std::string> &args)
{
	if(args.empty())
	{
		if(character->hasPlayer())
			std::cout << "What do you want to drop?" << std::endl;
		return;
	}

	if(args.size() == 1)
	{
		for(unsigned int i = 0; i < character->getItems().size(); i++)
		{
			//If argument is a character name, then look at the character.
			if(character->getItems()[i]->is(args[0]))
			{
				ItemPtr item = character->getItems()[i];
				character->remove(item);
				room->add(item);
				item->setStance(Item::LAYING);

				if(character->hasPlayer())
					std::cout << "You drop " << item->getIndefiniteArticle() << " " << item->getName() << " on the ground." << std::endl;
				else
					std::cout << character->getName() << " drops " << item->getIndefiniteArticle() << " " << item->getName() << " on the ground." << std::endl;
				return;
			}
		}

		if(character->hasPlayer())
		{
			std::cout << "You can't find that!" << std::endl;
			return;
		}
	}

	if(character->hasPlayer())
		std::cout << "You don't know how to do that!" << std::endl;
}
